using System.Collections.Generic;
using UnityEngine;


public class SocketDispatcher
{

	// Use this for initialization
	Dictionary<string, RegistSocketFunction> m_dispathcerMap = new Dictionary<string, RegistSocketFunction>();

	public void RegistEvent(string EventID, RegistSocketFunction pFunction)
	{
		if (!m_dispathcerMap.ContainsKey(EventID))
		{
			m_dispathcerMap.Add(EventID, pFunction);
		}
		else
		{
			m_dispathcerMap[EventID] += pFunction;
		}
	}

	public void UnRegistEvent(string EventID, RegistSocketFunction pFunction)
	{
		if (m_dispathcerMap.ContainsKey(EventID))
		{
			m_dispathcerMap[EventID] -= pFunction;
			if (m_dispathcerMap[EventID] == null)
			{
				m_dispathcerMap.Remove(EventID);
			}
		}
	}

	public void HandleEvent(string EventID, System.Object pSender)
	{
		RegistSocketFunction delegateEvent = null;
		if (m_dispathcerMap.TryGetValue(EventID, out delegateEvent))
		{
			if (delegateEvent == null)
				Debug.LogError("delegateEvent = null EventID:" + EventID);
			else
				delegateEvent(pSender);
		}
	}
}
